package com.ej.busi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ej.busi.domain.EjSubject;
import com.ej.busi.dto.subject.EjSubjectDto;
import com.ej.busi.mapper.EjSubjectMapper;
import com.ej.busi.service.IEjSubjectService;
import com.ej.common.exception.base.BaseException;
import com.ej.common.utils.StringUtils;
import com.ej.common.utils.uuid.IdUtils;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程表 服务层实现
 * 
 * @author ej
 */
@Service
public class EjSubjectServiceImpl extends ServiceImpl<EjSubjectMapper, EjSubject> implements IEjSubjectService {

    /**
     * 查询科目
     *
     * @param id 科目主键
     * @return 科目
     */
    @Override
    public EjSubject selectEjSubjectById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询科目列表
     *
     * @param ejSubject 科目
     * @return 科目
     */
    @Override
    public List<EjSubject> selectEjSubjectList(EjSubject ejSubject){

        LambdaQueryWrapper<EjSubject> queryWrapper = Wrappers.<EjSubject>lambdaQuery();
        if (ejSubject != null){
            if (ejSubject.getId() != null){
                queryWrapper.eq(EjSubject::getId, ejSubject.getId());
            }
            if (ejSubject.getName() != null){
                queryWrapper.eq(EjSubject::getName, ejSubject.getName());
            }
        }
        return this.list(queryWrapper);
    }

    /**
     * 新增科目
     *
     * @param ejSubject 科目
     * @return 结果
     */
    @Override
    public int insertEjSubject(EjSubject ejSubject)
    {
        String parentId = ejSubject.getParentId();
        if (StringUtils.isEmpty(parentId)){
            throw new BaseException("一级学科请联系管理员添加");
        }

        ejSubject.setCreateTime(LocalDateTime.now());
        ejSubject.setUpdateTime(LocalDateTime.now());
        this.save(ejSubject);
        return 1;
    }

    /**
     * 修改科目
     *
     * @param ejSubject 科目
     * @return 结果
     */
    @Override
    public int updateEjSubject(EjSubject ejSubject)
    {
        String parentId = ejSubject.getParentId();
        if (StringUtils.isEmpty(parentId)){
            throw new BaseException("一级学科请联系管理员添加");
        }
        ejSubject.setUpdateTime(LocalDateTime.now());
        this.updateById(ejSubject);
        return 1;
    }

    /**
     * 批量删除科目
     *
     * @param ids 需要删除的科目主键
     * @return 结果
     */
    @Override
    public int deleteEjSubjectByIds(Long[] ids)
    {
        this.removeBatchByIds(Lists.newArrayList(ids));
        return ids.length;
    }

    /**
     * 删除科目信息
     *
     * @param id 科目主键
     * @return 结果
     */
    @Override
    public int deleteEjSubjectById(Long id)
    {
        this.removeById(id);
        return 1;
    }

    @Override
    public List<EjSubjectDto> subjectList(Long parentId) {
        List<EjSubjectDto> result = Lists.newArrayList();
        if (parentId == null){
            parentId = 0L;
        }
        List<EjSubject> list = this.list(Wrappers.<EjSubject>lambdaQuery().eq(EjSubject::getParentId, parentId));
        list.forEach( subject -> {
            EjSubjectDto des = new EjSubjectDto();
            des.setSubjectId(subject.getId());
            des.setSubjectName(subject.getName());
            result.add(des);
        });
        return result;
    }
}
